package com.zhoudy.segmetlock;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class TransferDataTest {

    private static final int handlerThreads = 10;

    public static void main(String[] args) {
        long start =  System.currentTimeMillis();
        int arrLength = 1000;
        TransferData instance = new TransferData();
        String[] data = new String[arrLength];
        for (int i = 0; i < arrLength; i++) {
            data[i] = "TestData".concat(String.valueOf(i));
        }
        //迁移前
        System.out.println("老数据："+ Arrays.asList(data));

        //切分数据块
        instance.segmentData(data);

        //迁移开始并计数
        CountDownLatch countDownLatch = new CountDownLatch(handlerThreads);
        for (int i = 0; i < handlerThreads; i++) {
            //模拟并发迁移
            new Thread(new Worker(instance,countDownLatch)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //迁移后
        System.out.println("Finished:"+instance.isFinish());
        System.out.println("新数据："+instance.getNewDataContainer());
        System.out.println("耗时："+(System.currentTimeMillis() - start)+"ms");
    }
}
